package com.report.conversion.utils;

import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

/***
 * @description Excel 、 word 、 ppt 文件转 pdf 文件工具类
 * @return
 * @author kevin wu
 * @date 2023/3/13 14:25
 */
@Component
public class AsposeUtil {

    /***
     * @description 根据不同的类型获取License 暂时仅支持 ecel word ppt
     * @param type   1 excel  2  word  其他为 ppt
     * @return boolean
     * @author kevin wu
     * @date 2023/3/13 11:24
     */
    public static boolean getLicense(int type) {
        boolean result = false;
        try {
            InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            if (type == 1) {//excel
                com.aspose.cells.License aposeLic = new com.aspose.cells.License();
                aposeLic.setLicense(is);
                result = true;
            } else if (type == 2) {//word
                com.aspose.words.License aposeLic = new com.aspose.words.License();
                aposeLic.setLicense(is);
                result = true;
            } else {//ppt
                com.aspose.slides.License aposeLic = new com.aspose.slides.License();
                aposeLic.setLicense(is);
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /***
     * @description Excel转 pdf
     * @param officePath  原始文件全路径
     * @param OutPutPath  新文件路径
     * @param officeName  文件名称
     * @return void
     * @author kevin wu
     * @date 2023/3/13 11:26
     */
    public static void Excel2Pdf(String officePath,String OutPutPath,String officeName) {
        // 验证License
        if (!getLicense(1)) {
            return;
        }
        try {
            File file = new File(OutPutPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Workbook wb = new Workbook(officePath);// 原始excel路径
            File pdfFile = new File(OutPutPath+officeName);// 输出路径
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
            wb.save(fileOS, pdfSaveOptions);

            if(fileOS!=null){
                fileOS.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * @description word 转 pdf
     * @param officePath 原始文件全路径
     * @param OutPutPath 新文件路径
     * @param officeName 文件名
     * @return void
     * @author kevin wu
     * @date 2023/3/13 11:27
     */
    public static void Word2Pdf(String officePath,String OutPutPath,String officeName) {
        // 验证License
        if (!getLicense(2)) {
            return;
        }
        try {
            File file = new File(OutPutPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            Document doc = new Document(officePath);// 原始word路径
            File pdfFile = new File(OutPutPath+officeName);// 输出路径
            FileOutputStream fileOS = new FileOutputStream(pdfFile);
            doc.save(fileOS, com.aspose.words.SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * @description PPT 转 pdf
     * @param officePath 原始文件全路径
     * @param OutPutPath 新文件路径
     * @param officeName 文件名
     * @return void
     * @author kevin wu
     * @date 2023/3/13 11:28
     */
    public static void PPT2Pdf(String officePath,String OutPutPath,String officeName) {
        // 验证License
        if (!getLicense(3)) {
            return;
        }
        try {
            File PathFile = new File(OutPutPath);
            if (!PathFile.exists()) {
                PathFile.mkdirs();
            }
            InputStream slides = new FileInputStream(new File(officePath));// 原始ppt路径
            Presentation pres = new Presentation(slides);
            File file = new File(OutPutPath+officeName);// 输出pdf路径
            FileOutputStream fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * @description 文件转换
     * @param sourceFile 源文件全路径
     * @param outputPath 新文件输出路径
     * @param newFileName  新文件名称 为空的时候取源文件的名称
     * @return java.lang.String
     * @author kevin wu
     * @date 2023/3/13 14:31
     */
    public String conversionFile(String sourceFile,String outputPath,String newFileName){
        String filePath = "";
        try {

            int lastIndex = sourceFile.lastIndexOf(".");
            int lastNameIndex = sourceFile.lastIndexOf(File.separator);
            String officeType = sourceFile.substring(lastIndex+1);
            String officeName = "";
            if(newFileName==null || "".equals(newFileName)){
                officeName = sourceFile.substring(lastNameIndex+1,lastIndex)+".pdf";
            }else {
                officeName = newFileName;
            }
            if(outputPath==null || "".equals(outputPath)){
                outputPath = sourceFile.substring(0,lastNameIndex+1)+"office"+File.separator;
            }else {
                outputPath = outputPath.endsWith(File.separator)? outputPath : outputPath+File.separator;
            }

            if(officeType.equals("doc")||officeType.equals("docx")){
                Word2Pdf(sourceFile,outputPath,officeName);
            }else if(officeType.equals("xls")||officeType.equals("xlsx")){
                Excel2Pdf(sourceFile,outputPath,officeName);
            }else if(officeType.equals("ppt")||officeType.equals("pptx")){
                PPT2Pdf(sourceFile,outputPath,officeName);
            }
            filePath = outputPath+officeName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filePath;
    }
}
